<?php

/**
 * user_detail actions.
 *
 * @package    sf_sandbox
 * @subpackage user_detail
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */
class user_detailActions extends sfActions
{
  /**
   * Executes index action
   *
   */
    public function executeChangeYearPaid()
    {
        $this->year = $this->getRequestParameter('year');
        $this->phone = $this->getRequestParameter('phone');
    }
  public function executeIndex()
  {
    #$this->forward('default', 'module');
    if($this->hasRequestParameter('user')){
    	$u_infor = $this->getRequestParameter('user');
	    $u_infor = $this->formatPhone($u_infor);
    
    	// Get INFOR
    	
    	/*$con = new MysqlDAL('propel');
	    $sql = "SELECT id, username, phone, avatar_path, total_ringtone, created_datetime
	    		 FROM user 
	    		WHERE id = '{$u_infor}' OR username like '{$u_infor}%'";*/

    	$c = new Criteria;
    	$c1 = $c->getNewCriterion(UserPeer::ID, $u_infor);
		$c2 = $c->getNewCriterion(UserPeer::PHONE, $u_infor);
		$c1->addOr($c2);
		$c->add($c1);
	    $this->result = UserPeer::doSelectOne($c);
	    
	    if($this->result){
		    // GET BONUS
	    	$c = new Criteria();
			$c->add(AccountBonusPeer::USER_ID, $this->result->getId());
			$account= AccountBonusPeer::doSelectOne($c);
			$this->bonus = $account->getBonus();
			if($this->bonus == null){
				$this->bonus = 0;
			}
				 // Get so tien da duoc tra
	         $this->trathuong = TrathuongPeer::getPaidBonus($this->result->getPhone());
		         // Ngay tra gan nhat
	         $this->paidDate = date('d-m-Y', strtotime(TrathuongPeer::getLastPaid()));
	    	    // Get so tien thuong con lai (thang hien tai)
	         $this->curBonus = TrathuongPeer::getCurrentBonus($this->result->getPhone());
	         
	         // CAC DOWNLOAD
	         $this->_getdownload($this->result->getId()); 
	         
	         // ALL RINGTONE
	         $this->userId = $this->result->getId();
	         $this->type = 0;
	         $this->time = 0;
	         $this->page = 1;
	         $this->_getringtone($this->type,$this->userId,$this->time,$this->page);
	         
	         // IVR
	         $this->logIvr = $this->_getIVR($u_infor,1);
	    }
    }
  }
  
  public function executeReloadRtList()
  {
  	$this->userId = $this->getRequestParameter('userId');
  	$this->type = $this->getRequestParameter('type');
  	$this->time = $this->getRequestParameter('time');
  	$this->page = $this->getRequestParameter('page');
  	$this->_getringtone($this->type,$this->userId,$this->time,$this->page);
  } 
  
  public function executeReloadDownList()
  {
  	$this->userId = $this->getRequestParameter('userId');
  	$this->type = $this->getRequestParameter('type');
  	$this->time = $this->getRequestParameter('time');
  	$this->page = $this->getRequestParameter('page');
  	
  	$this->_getdownload($this->userId,$this->page); 
  }
  
  public function executeFilterDownload()
  {
  	$limit = 10;
  	$this->userId  = $this->getRequestParameter('userId');
  	$this->d_page = $this->getRequestParameter('page');;
  	
  	$this->from = $this->getRequestParameter('from');
  	$this->to = $this->getRequestParameter('to');
  	
  	$c0 = new Criteria();
    $c0 ->add(UserPeer::ID, $this->userId);
    $user = UserPeer::doSelectOne($c0);
  	
  	$c = new Criteria();
    #$c->add(RingtoneDownloadPeer::USER_ID,$this->userId);
    $c->add(RingtoneDownloadPeer::FROM_PHONE,$user->getPhone());
    $c->add(RingtoneDownloadPeer::DOWNLOAD_DATETIME,$this->to,Criteria::LESS_EQUAL);
    $c->add(RingtoneDownloadPeer::DOWNLOAD_DATETIME,$this->from,Criteria::GREATER_EQUAL);
    
  	$totalrecord = RingtoneDownloadPeer::doCount($c);
  	
  	$this->d_totalpage = iring::getTotalPage($totalrecord, $limit);
  	
  	if($this->d_page > $this->d_totalpage && $this->d_totalpage>0){
		$this->d_page = $this->totalPage;
	}
	
  	$offset = ($this->d_page-1)*$limit;
  	$c = new Criteria();
    #$c->add(RingtoneDownloadPeer::USER_ID,$this->userId);
    $c->add(RingtoneDownloadPeer::FROM_PHONE,$user->getPhone());
    $c->add(RingtoneDownloadPeer::DOWNLOAD_DATETIME,$this->to,Criteria::LESS_EQUAL);
    $c->add(RingtoneDownloadPeer::DOWNLOAD_DATETIME,$this->from,Criteria::GREATER_EQUAL);
    $c->setLimit($limit);
    $c->setOffset($offset);
    $this->user_download =RingtoneDownloadPeer::doSelect($c);
    
  }
  public function executeIvrnextpage()
  {
  	$userPhone = $this->getRequestParameter("userPhone");
  	$page = $this->getRequestParameter("page");
  	$this->logIvr = $this->_getIVR($userPhone,$page);
  }
  
  protected function formatPhone($phone){
  	if(strpos($phone,"84")!==0){
        if(strpos($phone,"0")!==0) $phone = "84".$phone;
        else $phone = "84".substr($phone,"1");
    }
  	return $phone;
  }
  
  protected  function _getringtone($type,$userId,$time,$page=1)
  {
  	$limit = 10;
  	$this->page = $page;
  	switch ($type)
		{
			case 0:
				$this->totalRt = UserPeer::getTotalRingtoneByUserId($userId);
				$this->totalPage = iring::getTotalPage($this->totalRt, $limit);
				
				if($this->page > $this->totalPage && $this->totalPage>0){
					$this->page = $this->totalPage;
				}
				
				$offset = ($this->page-1)*$limit;
				$this->ringtones= UserPeer::getRingtoneByUserId($userId, $time, $limit, $offset);
				break;
			case 1: 
				$this->totalRt = UserPeer::getTotalRingtoneByUserIdUsed($this->userId);
				$this->totalPage = iring::getTotalPage($this->totalRt, $limit);
				if($this->page > $this->totalPage && $this->totalPage>0){
					$this->page = $this->totalPage;
				}
				$offset = ($this->page-1)*$limit;
				$this->ringtones= UserPeer::getRingtoneByUserIdUsed($userId, $time, $limit, $offset);
				break;
			case 2:
				$this->totalRt = UserPeer::getTotalNotApprovedRingtone($userId);
				$this->totalPage = iring::getTotalPage($this->totalRt, $limit);
				if($this->page > $this->totalPage && $this->totalPage>0){
					$this->page = $this->totalPage;
				}
				$offset = ($this->page-1)*$limit;
				$this->ringtones= UserPeer::getNotApprovedRingtone($userId, $time, $limit, $offset);
				break;
			case 3:
				$this->totalRt = UserPeer::getTotalDeleteRingtoneByAdmin($userId);
				$this->totalPage = iring::getTotalPage($this->totalRt, $limit);
				
				if($this->page > $this->totalPage && $this->totalPage>0){
					$this->page = $this->totalPage;
				}
				$offset = ($this->page-1)*$limit;
				$this->ringtones= UserPeer::getDeleteRingtoneByAdmin($userId, $time, $limit, $offset);
				break;
			case 4:
				$this->totalRt = UserPeer::getTotalDeleteRingtone_rbtByAdmin($userId);
				$this->totalPage = iring::getTotalPage($this->totalRt, $limit);
				
				if($this->page > $this->totalPage && $this->totalPage>0){
					$this->page = $this->totalPage;
				}
				$offset = ($this->page-1)*$limit;
				$this->ringtones= UserPeer::getDeleteRingtone_rbtByAdmin($userId, $time, $limit, $offset);
				break;
		}
	
  }
  
  protected function _getdownload($userid, $page=1)
  {
  	$limit = 10;
  	$this->d_page = $page;
  	$this->userId = $userid;
  	
  	$c0 = new Criteria();
    $c0 ->add(UserPeer::ID, $userid);
    $user = UserPeer::doSelectOne($c0);
	    
  	$c = new Criteria();
    $c->add(RingtoneDownloadPeer::FROM_PHONE,$user->getPhone());
  	$totalrecord = RingtoneDownloadPeer::doCount($c);
  	
  	$this->d_totalpage = iring::getTotalPage($totalrecord, $limit);;
  	
  	if($this->d_page > $this->d_totalpage && $this->d_totalpage>0){
		$this->d_page = $this->totalPage;
	}
	
  	$offset = ($this->d_page-1)*$limit;
  	$c = new Criteria();
    $c->add(RingtoneDownloadPeer::FROM_PHONE,$user->getPhone());
    $c->setLimit($limit);
    $c->setOffset($offset);
    $this->user_download =RingtoneDownloadPeer::doSelect($c);
  }
  
  protected function _getIVR($userPhone, $page)
  {
  	$limit = 10;
  	$this->ivr_page = $page;
  	$this->userPhone = $this->formatPhone($userPhone);
  	$c = new Criteria();
  	$c->add(IvrLogPeer::PHONE,$this->userPhone);
  	
  	$totalrecord = IvrLogPeer::doCount($c);
  	$this->ivr_totalpage = ceil($totalrecord/$limit);
  	if($this->ivr_page > $this->ivr_totalpage && $this->ivr_totalpage>0){
		$this->ivr_page = $this->ivr_totalpage;
	}
	if($this->ivr_page < 1){
		$this->ivr_page = 1;
	}
	
  	$offset = ($this->ivr_page-1) * $limit;
  	
  	$c->addDescendingOrderByColumn(IvrLogPeer::ID);
  	$c->setLimit($limit);
  	$c->setOffset($offset);
  	return IvrLogPeer::doSelect($c);
  	
  }
}
